package com.lp;

import com.lp.entity.ListNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SolutionTest {
    public static void main(String[] args) {
//        ListNode head = new ListNode(1);
//        head.next = new ListNode(2);
//        head.next.next = new ListNode(3);
//        ListNode listNode = middleNode(head);
//-------------------------------------------------------------------------------
//        int[][] grid ={{1,2},{3,4}};
//        int i = surfaceArea(grid);
//        System.out.println(i);
//-------------------------------------------------------------------------------
//        char[][] board = {{'.','.','.','.','.','.','.','.'},
//                        {'.','.','.','p','.','.','.','.'},
//                        {'.','.','.','R','.','.','.','p'},
//                        {'.','.','.','.','.','.','.','.'},
//                        {'.','.','.','.','.','.','.','.'},
//                        {'.','.','.','p','.','.','.','.'},
//                        {'.','.','.','.','.','.','.','.'},
//                        {'.','.','.','.','.','.','.','.'}};
//        boolean equals = "R".charAt(0)==board[2][3];
//        System.out.println(equals);
//        int i = numRookCaptures(board);
//        System.out.println(i);
//-------------------------------------------------------------------------------
//        int[] deck = new int[]{0,0,0,0,0,0,0,1,2,3,3,3,4,5,6};
//        boolean b = hasGroupsSizeX(deck);
//        System.out.println(b);
//-------------------------------------------------------------------------------
//        int i = lastRemaining(70866, 116922);
//        System.out.println(i);
//-------------------------------------------------------------------------------
//        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
//        int[][] rotate = rotate(matrix);
//        System.out.println(rotate);
//-------------------------------------------------------------------------------
        String s = "  hello world!  ";
        String s1 = reverseWords(s);
        System.out.println(s1);
    }

    public static ListNode middleNode(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    public static int surfaceArea(int[][] grid) {
        int count = 0;
        int c = 0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++){
                count+=grid[i][j];
                if(grid[i][j] > 0){
                    c+=(grid[i][j]-1)*2;
                }
                if(j>0){
                    if(grid[i][j]> 0 && grid[i][j-1]>0){
                            c+=Math.min(grid[i][j],grid[i][j-1])*2;
                    }
                }
                if(i>0){
                    if(grid[i][j]> 0 && grid[i-1][j]>0){
                            c+=Math.min(grid[i][j],grid[i-1][j])*2;
                    }
                }
            }
        }
        return 6*count-c;
    }
    public static int numRookCaptures(char[][] board) {
        int rx = 0;
        int ry = 0;
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){
                if("R".equals(String.valueOf(board[i][j]))){
                    rx = i;
                    ry = j;
                    break;
                }
            }
        }
        int count = 0;
        for(int i=ry-1;i>0;i--){
            if(!"B".equals(String.valueOf(board[rx][i]))){
                if("p".equals(String.valueOf(board[rx][i]))){
                    count++;
                    break;
                }
            }else{
                break;
            }
        }
        for(int i=ry+1;i<8;i++){
            if(!"B".equals(String.valueOf(board[rx][i]))){
                if("p".equals(String.valueOf(board[rx][i]))){
                    count++;
                    break;
                }
            }else{
                break;
            }
        }
        for(int i=rx-1;i>0;i--){
            if(!"B".equals(String.valueOf(board[i][ry]))){
                if("p".equals(String.valueOf(board[i][ry]))){
                    count++;
                    break;
                }
            }else{
                break;
            }
        }
        for(int i=rx+1;i<8;i++){
            if(!"B".equals(String.valueOf(board[i][ry]))){
                if("p".equals(String.valueOf(board[i][ry]))){
                    count++;
                    break;
                }
            }else{
                break;
            }
        }
        return count;
    }
    public static boolean hasGroupsSizeX(int[] deck) {
        if(deck.length < 2){
            return false;
        }
        Map<Integer,Integer> map =new HashMap<>();
        for(int i=0;i<deck.length;i++){
            if(map.get(deck[i])!=null){
                map.put(deck[i],map.get(deck[i])+1);
            }else{
                map.put(deck[i],1);
            }
        }
        int x = 0;
        List<Integer> list = new ArrayList<>();
        for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            int val = entry.getValue();
            x = gcd(x,val);
            if(x == 1){
                return false;
            }
        }
        return x >= 2;
    }
    private static int gcd (int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
    public static int lastRemaining(int n, int m) {
        List<Integer> list = new ArrayList<>();
        for(int i =0;i<n;i++){
            list.add(i);
        }
        int count = 0;
        int index = 0;
        while(true){
            count++;
            if(count == m){
                list.remove(index);
                index--;
                count = 0;
            }
            if(index == list.size()-1){
                index = 0;
            }else{
                index++;
            }
            if(list.size() == 1){
                break;
            }
        }
        return list.get(0);
    }
    public static int[][] rotate(int[][] matrix) {
        int[][] tempMatrix = new int[matrix.length][matrix.length];
        int col = matrix.length-1;
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix.length;j++){
                tempMatrix[j][col] = matrix[i][j];
            }
            col--;
        }
        matrix = tempMatrix;
        return matrix;
    }
    public static String reverseWords(String s) {
        String[] split = s.split(" ");
        List<String> list = new ArrayList<>();
        for(int j=0;j<split.length;j++){
            if("".equals(split[j])){
                continue;
            }
            list.add(split[j].trim());
        }

        StringBuffer sb = new StringBuffer();
        for(int i=list.size()-1;i>=0;i--){
            if(i == 0){
                sb.append(list.get(i));
            }else{
                sb.append(list.get(i)+" ");
            }
        }
        return sb.toString();
    }
}
